package com.orangelife.mobile.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.main.activity.AdShow;
import com.orangelife.mobile.main.biz.JumpUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/16.
 */
@Deprecated
public class TopRecommendAdapter<T> extends FatherAdapter<T> {

    private Context mContext;
    private List<Map<String, Object>> list = null;
    private DisplayImageOptions options;

    public TopRecommendAdapter(Context context,
                               List<Map<String, Object>> list) {
        this.mContext = context;
        this.list = list;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_banner)
                .showImageForEmptyUri(R.drawable.default_banner)
                .showImageOnFail(R.drawable.default_banner)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(20))
                .build();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_topshop, null);

            listItemView.tvContent = (TextView) convertView
                    .findViewById(R.id.tv_text);
            listItemView.ivTopShop = (ImageView) convertView
                    .findViewById(R.id.iv_shop);

            listItemView.llTopShop = (LinearLayout) convertView
                    .findViewById(R.id.ll_top);

            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
        Map<String, Object> map = list.get(position);
        //image.download(map.get("icon")+"",listItemView.ivTopShop, ImageDownloader.ImageSize.IMAGE_SIZE_SMALL_JPG);

        ImageLoader.getInstance().displayImage(map.get("icon") + "", listItemView.ivTopShop, options);
        if (map.get("restTime").equals("")) {
            listItemView.tvContent.setVisibility(View.INVISIBLE);
        }
        listItemView.tvContent.setText(map.get("restTime") + "");
        listItemView.llTopShop.setOnClickListener(new listener(map));
        return convertView;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return TopRecommendAdapter.this;
    }

    class listener implements View.OnClickListener {
        Map<String, Object> map = null;

        public listener(Map<String, Object> map) {
            this.map = map;
        }

        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse(map.get("url").toString());
            if (uri.getScheme().equals("orangelife") && uri.getHost().equals("www.com")) {
//                ExplainUrlUtils.explainUrl(uri, mContext, true);
                JumpUtil.jumpActivity(mContext, map.get("url").toString(), true);
            } else {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("advertID", "");
                bundle.putString("content", map.get("content") + "");
                bundle.putInt("type", Integer.parseInt(map.get("type").toString()));
                bundle.putString("body", "");
                bundle.putString("url", map.get("url") + "");
                bundle.putString("advertType", "");
                bundle.putString("linkID", "");
                bundle.putString("title", map.get("title") + "");
                intent.setClass(mContext, AdShow.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        }
    }

    public final class ListItemView {
        /** 商品说明 */
        public TextView tvContent;
        /** 商平图片 */
        public ImageView ivTopShop;
        /** 点击item */
        public LinearLayout llTopShop;
    }
}
